﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Apps.Console.ViewModel;

namespace Mimoza.Apps.Console.Commands
{
    class EditPolicyCommand : BaseConsoleCommand
    {
        PolicyViewModel m_ViewModel = null;
        public EditPolicyCommand(PolicyViewModel viewModel)
        {

            m_IsDefault = true;
            m_ViewModel = viewModel;
            m_ViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.view_PropertyChanged);
        }

        void view_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (null == m_ViewModel) return;

            this.Visible = m_ViewModel.InFocus;
            if (null != m_ViewModel.Context && null != m_ViewModel.CurrentContainer)
            {
                Mimoza.ObjectLayer.Policy policy = m_ViewModel.Context as Mimoza.ObjectLayer.Policy;
                if (null != policy && null != policy.Parent && policy.Parent.ID == m_ViewModel.CurrentContainer.ID)
                {
                    this.Enabled = true;
                }
                else
                {
                    this.Enabled = false;
                }
            }
            else
            {
                this.Enabled = false;
            }
        }
        public override void Execute()
        {
            if (null == m_ViewModel) return;

            Mimoza.ObjectLayer.Policy policy = m_ViewModel.Context as Mimoza.ObjectLayer.Policy;

            if (null != policy)
            {
                PolicyWizard wizard = new PolicyWizard(m_ViewModel.Application, policy);
                wizard.Init(Mimoza.GUI.WizardMode.PropertySheet);
                if (System.Windows.Forms.DialogResult.OK == wizard.Start())
                {
                    policy.DataModificationID = Guid.NewGuid();
                    m_ViewModel.Application.ObjectLayer.PolicyRepository.Update(policy);
                }
            }
        }
    }
}
